import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 我这样虽然ac,但是一定不能排序，排序也改变了原数组的值
 */
public class LongestConsecutiveSequence2 {
    public static void main(String[] args) {
        int[] nums={100,4,200,1,3,2};
        System.out.println(longestConsecutive(nums));
    }
    public static int longestConsecutive(int[] nums) {

        Set<Integer> hashSet=new HashSet<>();
        for (int num : nums) {
            hashSet.add(num);
        }
        int longest=0;
        for (Integer num : hashSet) {
            if(!hashSet.contains(num-1)){// 细节减少外循环次数，当前数是最小才继续判断，避免重复判断，
                int curLen=1;
                while(hashSet.contains(++num)){
                    curLen++;
                }
                longest=Math.max(curLen,longest);
            }
        }
        return longest;
    }
}
